package com.sc.nft.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.sc.nft.dao.ApiOrderDirectDao;
import com.sc.nft.entity.ApiOrderDirect;
import com.sc.nft.enums.ApiOrderStateEnum;
import com.sc.nft.enums.ApiOrderTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ApiOrderDirectService;
import com.sc.nft.sup.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 福禄订单业务层
 *
 * @author liGen
 * @version 1.0
 * @date 2023/11/15 1:56 下午
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ApiOrderDirectServiceImpl implements ApiOrderDirectService {

    private final ApiOrderDirectDao apiOrderDirectDao;

    /**
     * 创建福禄订单
     *
     * @param orderId          订单ID
     * @param orderNo          订单编号
     * @param apiProductId     福禄商品ID
     * @param apiChargeAccount 充值账号
     * @param apiBuyNum        购买数量
     * @return
     */
    @Override
    public ApiOrderDirect create(Long orderId, String orderNo, Integer apiProductId, String apiChargeAccount, Integer apiBuyNum) {
        ApiOrderDirect orderDirect = getByOrderId(orderId);
        if (Objects.nonNull(orderDirect)) {
            new GlobalRunTimeException(ErrorCode.THE_DIRECT_CHARGE_ORDER_ALREADY_EXISTS);
        }
        ApiOrderDirect apiOrderDirect = new ApiOrderDirect();
        apiOrderDirect.setOrderId(orderId);
        apiOrderDirect.setOrderNo(orderNo);
        apiOrderDirect.setApiProductId(apiProductId);
        apiOrderDirect.setApiChargeAccount(apiChargeAccount);
        apiOrderDirect.setApiBuyNum(apiBuyNum);
        apiOrderDirect.setApiOrderType(ApiOrderTypeEnum.DIRECT_CHARGE.getCode());
        apiOrderDirect.setApiOrderState(ApiOrderStateEnum.UNTREATED.getCode());
        apiOrderDirect.insert();
        return apiOrderDirect;
    }

    @Override
    public ApiOrderDirect getByOrderId(Long orderId) {
        return apiOrderDirectDao.getByOrderId(orderId);
    }

    @Override
    public ApiOrderDirect getByOrderNo(String orderNo) {
        return apiOrderDirectDao.getByOrderNo(orderNo);
    }

    @Override
    public List<ApiOrderDirect> getByApiOrderState(ApiOrderStateEnum apiOrderStateEnum) {
        return apiOrderDirectDao.getByApiOrderState(apiOrderStateEnum);
    }

    @Override
    public String synchronizationOrderDirect(JSONObject resultJson) {
        if (Objects.isNull(resultJson)) {
            return "fail";
        }
        String orderNo = resultJson.getString("customer_order_no");
        log.info("customer_order_no:{}",orderNo);
        String chargeFinishTime = resultJson.getString("charge_finish_time");
        String orderStatus = resultJson.getString("order_status");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date successTime = null;
        try {
            successTime = sdf.parse(chargeFinishTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ApiOrderDirect apiOrderDirect = getByOrderNo(orderNo);
        if (Objects.nonNull(apiOrderDirect)){
            apiOrderDirect.setApiOrderState(orderStatus);
            apiOrderDirect.setSuccessTime(successTime);
            apiOrderDirect.updateById();
            return "success";
        }else {
            return "fail";
        }
    }

    @Override
    public void automaticFailure(Integer day) {
        List<ApiOrderDirect> apiOrderDirects = getByApiOrderState(ApiOrderStateEnum.UNTREATED);
        if (!CollectionUtils.isEmpty(apiOrderDirects)){
            for (ApiOrderDirect apiOrderDirect : apiOrderDirects) {
                try {

                }catch (Exception e){
                    log.error("直充订单状态自动失败 :{}",apiOrderDirect, e);
                }
                Date createTime = apiOrderDirect.getCreateTime();
                DateTime dateTime = DateUtil.offsetDay(createTime, day);
                if (DateTime.now().isAfter(dateTime)) {
                    apiOrderDirect.setApiOrderState(ApiOrderStateEnum.FAILED.getCode());
                    apiOrderDirect.updateById();
                }
            }
        }
    }
}
